﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.Client;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Sale
{
    public class AddSaleHandler : IRequestHandler<AddSaleCommand, Result>
    {
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 客户仓储
        /// </summary>
        public readonly IBaseRepository<Sales> salesRepository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<AddSaleHandler> logger;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="salesRepository"></param>
        /// <param name="logger"></param>
        public AddSaleHandler(IMapper mapper, IBaseRepository<Sales> salesRepository, ILogger<AddSaleHandler> logger)
        {
            this.mapper = mapper;
            this.salesRepository = salesRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 销售添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(AddSaleCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            Sales Sales = mapper.Map<Sales>(request);
            var res = await salesRepository.Add(Sales);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "客户添加成功";
            }
            return result;
        }
    }
}
